# coding=utf-8
from django.http import HttpResponse

import json

class _CheckLogin(object):
	def __init__(self,func,check,cmd,exceptions):
		self.func = func
		self.check = check
		self.cmd = cmd
		self.exceptions = exceptions
	def __call__(self,request,*args,**kwargs):
		if self.cmd and self.exceptions and type:
			cmd = request.REQUEST.get(self.cmd,'').strip()
			if cmd in self.exceptions:
				return self.func(request,*args,**kwargs)
		if self.check(request.user):
			return self.func(request,*args,**kwgars)
		return HttpResponse(json.dumps({'code':-1,'html':'请先登陆','script':'cmdAssist("login");'}))

class login_required(object):
	def __init__(self,cmd='',exceptions=[]):
		self.cmd = cmd
		self.exceptions = exceptions
	def __call__(self,func):
		return _CheckLogin(func,
			lambda u : u.is_authenticated(),
			self.cmd,
			self.exceptions)
